package com.begye.smartloan.apptest.tool

import android.util.Base64

object DingDangTool {

    const val ddCreated = "Q1JFQVRFRA"
    const val ddJuJue = "REVOSUVE"
    const val ddFangKuanZhong = "TEVORElORw=="
    const val ddDaiHuanKuan = "TE9BTkVE"
    const val ddYuQi = "T1ZFUkRVRQ=="
    const val ddHuaiZhang = "QkFEX0RFQlQ="

    const val sqCreated = "Q1JFQVRFRA=="
    const val sqXiTongShenHeZhong = "U1lTX0NIRUNL"
    const val sqXiTongJuJue = "U1lTX0RFTklFRA=="
    const val sqXiTongTongGuo = "U1lTX0FQUk9WQUw="
    const val sqRenGongJuJue = "TUFOVV9ERU5JRUQ="
    const val sqRenGongTongGuo = "TUFOVV9BUFJPVkFM"
    const val sqDianShenJuJue = "RFNfREVOSUVE"
    const val sqDianShenTongGuo = "RFNfQVBST1ZBTA=="

    fun checkStatus(shenQing: String, dingDan: String): PageTagTool? {
        return try {
            val shenQingStatusBys = shenQing.toByteArray()
            val shenQingStatusB64 =
                Base64.encodeToString(shenQingStatusBys, Base64.DEFAULT or Base64.NO_WRAP)

            val dingDanStatusBys = dingDan.toByteArray()
            val dingDanStatusB64 =
                Base64.encodeToString(dingDanStatusBys, Base64.DEFAULT or Base64.NO_WRAP)

            if (matchDaiHuanKuanStatus(dingDanStatusB64)) {
                return PageTagTool.dai_huan_kuan
            }
            if (matchFangKuanZhongStatus(dingDanStatusB64)) {
                return PageTagTool.fang_kuan_zhong
            }
            if (matchShenHeZhongStatus(shenQingStatusB64)) {
                return PageTagTool.shen_he_zhone
            }
            if (matchJuJueStatus(shenQingStatusB64, dingDanStatusB64)) {
                return PageTagTool.ju_jue
            }
            null
        } catch (e: Exception) {
            //
            null
        }
    }

    fun matchDaiHuanKuanStatus(value: String): Boolean {
        return (value == ddDaiHuanKuan) or (value == ddYuQi) or (value == ddHuaiZhang)
    }

    fun matchShenHeZhongStatus(value: String): Boolean {
        return ((value == sqCreated) or (value == sqXiTongShenHeZhong) or (value == sqXiTongTongGuo)
                or (value == sqRenGongTongGuo) or (value == sqDianShenTongGuo))
    }

    fun matchJuJueStatus(shenQing: String, dingDan: String): Boolean {
        return matchDingDanJuJueStatus(dingDan) or matchShenQingJuJueStatus(shenQing)
    }

    fun matchFangKuanZhongStatus(value: String): Boolean {
        return (value == ddFangKuanZhong) or (value == ddCreated)
    }

    private fun matchDingDanJuJueStatus(value: String): Boolean {
        return (value == ddJuJue)
    }

    private fun matchShenQingJuJueStatus(value: String): Boolean {
        return (value == sqXiTongJuJue) or (value == sqRenGongJuJue) or (value == sqDianShenJuJue)
    }

    fun hasDingDanStatus(value: String): Boolean {
        return try {
            val waitStr = value.toByteArray()
            val result = Base64.encodeToString(waitStr, Base64.DEFAULT or Base64.NO_WRAP)

            val list =
                mutableListOf(
                    ddCreated,
                    ddJuJue,
                    ddFangKuanZhong,
                    ddDaiHuanKuan,
                    ddYuQi,
                    ddHuaiZhang
                )
            list.any {
                result == it
            }
        } catch (e: Exception) {
            //
            false
        }
    }

    fun hasShenQingStatus(value: String): Boolean {
        return try {
            val waitStr = value.toByteArray()
            val result = Base64.encodeToString(waitStr, Base64.DEFAULT or Base64.NO_WRAP)

            val list =
                mutableListOf(
                    sqCreated,
                    sqXiTongShenHeZhong,
                    sqXiTongJuJue,
                    sqXiTongTongGuo,
                    sqRenGongTongGuo,
                    sqRenGongJuJue,
                    sqDianShenTongGuo,
                    sqDianShenJuJue
                )
            list.any {
                result == it
            }
        } catch (e: Exception) {
            //
            false
        }
    }


    fun mainTest() {

        val list1 = mutableListOf(
            "CREATED",
            "SYS_CHECK",
            "SYS_DENIED",
            "SYS_APROVAL",
            "MANU_APROVAL",
            "MANU_DENIED",
            "DS_APROVAL",
            "DS_DENIED"
        )

        val list2 = mutableListOf("CREATED", "DENIED", "LENDING", "LOANED", "OVERDUE", "BAD_DEBT")

        println("===========list1 encode===============")
        for (index in 0 until list1.size) {
            val item = list1[index]
            val waitStr = item.toByteArray()
            val encode = Base64.encodeToString(waitStr, Base64.DEFAULT or Base64.NO_WRAP)

            println("$item<-->${encode}<-->")
        }

        println("===========list2 encode===============")
        for (index in 0 until list2.size) {
            val item = list2[index]
            val waitStr = item.toByteArray()
            val encode = Base64.encodeToString(waitStr, Base64.DEFAULT or Base64.NO_WRAP)

            println("$item<-->${encode}<-->")
        }

        val list3 = mutableListOf(
            "Q1JFQVRFRA==",
            "U1lTX0NIRUNL",
            "U1lTX0RFTklFRA==",
            "U1lTX0FQUk9WQUw=",
            "TUFOVV9BUFJPVkFM",
            "TUFOVV9ERU5JRUQ=",
            "RFNfQVBST1ZBTA==",
            "RFNfREVOSUVE"
        )

        val list4 = mutableListOf(
            "Q1JFQVRFRA",
            "REVOSUVE",
            "TEVORElORw==",
            "T1ZFUkRVRQ==",
            "T1ZFUkRVRQ==",
            "QkFEX0RFQlQ="
        )

        println("===========list3 decode===============")
        for (index in 0 until list3.size) {
            val item = list3[index]
            val bytes = Base64.decode(item, Base64.DEFAULT or Base64.NO_WRAP)
            val decode = String(bytes, Charsets.UTF_8)
            println("$item<-->${decode}<-->")
        }

        println("===========list4 decode===============")
        for (index in 0 until list4.size) {
            val item = list4[index]
            val bytes = Base64.decode(item, Base64.DEFAULT or Base64.NO_WRAP)
            val decode = String(bytes, Charsets.UTF_8)
            println("$item<-->${decode}<-->")
        }
    }

}